﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class QL_Banner : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter ad;
    DataTable dt = new DataTable();
    private string Connect = ConfigurationManager.ConnectionStrings["ketnoi"].ConnectionString;
    string sql = "select * from banner";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"].ToString() == "")
        {
            Response.Redirect("Trang_Chu.aspx");
        }
        else
        {

            if (!IsPostBack)
            {
                Hien_Thi();
                if (dt.Rows.Count > 0)
                {
                    TextBox1.Text = dt.Rows[0]["ID"].ToString();
                    TextBox2.Text = dt.Rows[0]["HINH"].ToString();
                }
                btnluu.Visible = false;
                btnkluu.Visible = false;
                TextBox1.ReadOnly = TextBox2.ReadOnly = true;

            }
        }
    }
    public void Hien_Thi()
    {
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            ad = new SqlDataAdapter(sql, con);
            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                Label1.Text = "Không có dữ liệu";
            }
        }
        catch (Exception ex)
        {
            Label1.Text = "Lỗi: " + ex.Message;
        }
    }
    protected void btbhinh_Click(object sender, EventArgs e)
    {
        HttpPostedFile file = FileUpload1.PostedFile;
        if (FileUpload1.HasFile == false || file.ContentLength > 800000)
        {
            lblhinh.Text = "Chọn file ảnh khác";
        }
        else
        {
            try
            {
                string strPath = Server.MapPath("~/images/" + FileUpload1.FileName);
                FileUpload1.SaveAs(strPath);
                TextBox2.Text = "~/images/" + FileUpload1.FileName.ToString();
                lblhinh.Text = "Upload thành công";
            }
            catch
            {
                lblhinh.Text = "Upload thất bại";
            }
        }
    }
    protected void btbthem_Click(object sender, EventArgs e)
    {
        TextBox1.ReadOnly = true;
        TextBox1.Text = TextBox2.Text ="";
        Label1.Text = "";        
        btbthem.Visible = false;
        btnsua.Visible = false;
        btnxoa.Visible = false;
        btnluu.Visible = true;
        btnkluu.Visible = true;
        Session["Them"] = 1;
    }
    protected void btnsua_Click(object sender, EventArgs e)
    {
        Label1.Text = "";        
        TextBox1.ReadOnly = true;
        btbthem.Visible = false;
        btnsua.Visible = false;
        btnxoa.Visible = false;
        btnluu.Visible = true;
        btnkluu.Visible = true;
        Session["Them"] = 2;        
    }
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "delete from BANNER where ID =" + TextBox1.Text;
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                Label1.Text = "Đã Xóa Thành Công";
                Hien_Thi();
                if (dt.Rows.Count > 0)
                {
                    TextBox1.Text = dt.Rows[0]["ID"].ToString();
                    TextBox2.Text = dt.Rows[0]["HINH"].ToString();
                    
                }
                else
                {
                    Label1.Text = "Không xóa được";
                }
            }
            con.Dispose();
            con.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = "Lỗi:" + ex.Message;
        }
    }
    protected void btnluu_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            string sql = "";
            cmd = new SqlCommand();
            cmd.Connection = con;
            if (Session["Them"].ToString() == "1")
            {
                sql = "insert into BANNER (HINH,NGAYCAPNHAT)" + "values('" + TextBox2.Text + "','"+DateTime.Now.ToString()+"')";
            }
            else if (Session["Them"].ToString() == "2")
            {
                sql = "update BANNER set HINH='" + TextBox2.Text + "',NGAYCAPNHAT='"+DateTime.Now.ToString()+"'where ID='" + TextBox1.Text + "'";
            }
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                Label1.Text = "Thực hiện thành công";
                btbthem.Visible = true;
                btnsua.Visible = true;
                btnxoa.Visible = true;
                btnluu.Visible = false;
                btnkluu.Visible = false;
                Hien_Thi();
            }
            else
            {
                Label1.Text = "Thực hiện không thành công";
            }
            con.Dispose();
            con.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = "Lỗi: " + ex.Message;
        }
    }
    protected void btnkluu_Click(object sender, EventArgs e)
    {
        btbthem.Visible = true;
        btnxoa.Visible = true;
        btnsua.Visible = true;
        btnluu.Visible = false;
        btnkluu.Visible = false;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int i = GridView1.SelectedIndex;
        TextBox1.Text = GridView1.Rows[i].Cells[0].Text;
        TextBox2.Text = GridView1.Rows[i].Cells[1].Text;
    }
}
